﻿using ProjectCloth.Entity;
using ProjectCloth.Entity.Enum;
using ProjectCloth.FrmUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectCloth.FrmUI
{
    public partial class LoginFrm : Form
    {
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void loginBtn_Click(object sender, EventArgs e)
        {
            loginBtn.BackColor = Color.DarkGray;
            //切换楼层是不是要重新登录
            //var firstList = new List<string>()
            //{
            //       "so",
            //       "福",
            //       "丽",
            //       "芳",
            //       "小马",
            //       "小妹",
            //       "鑫",
            //       "永发"
            //};
            var Catalog = "";
            //if (firstList.Where(o => o == userTxt.Text.Trim()).Count() > 0)
            //Catalog = "clothdb0401";
            if (comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择楼层！");
                return;
            }
            if (comboBox1.SelectedItem.ToString() == "一楼")
                Catalog = "clothdb0401";
            else Catalog = "clothdb0402";
            //Catalog = "clothdb040101";
            //else Catalog = "clothdb0623";
            ClothContext.connection = $"Data Source=212.64.54.64;port=3306;Initial Catalog={Catalog};uid=root;password=root@cloth;Charset=utf8";
            ClothContext db = new ClothContext();
            var user = db.Users.Where(o => o.LoginName == userTxt.Text.Trim() && o.Password == userPwd.Text.Trim()).FirstOrDefault();
            if (user != null)
            {
                var rolestr = (user.RoleIdStr + "").Split(',');
                var rolePerList = db.Roles.Where(o => rolestr.Contains(o.Id.ToString())).Select(o => o.PermissionStr).ToList();
                var permissosStr = new List<string>();
                foreach (var p in rolePerList)
                {
                    if (!string.IsNullOrEmpty(p))
                    {
                        var arrList = p.Split(',');
                        permissosStr.AddRange(arrList.ToList());
                    }
                }
                var permissions = db.Permissions.Where(o => permissosStr.Contains(o.Id.ToString())).ToList();
                AddTab.Permission = permissions.Select(o => o.Name).ToList();
                if (!user.Enabled)
                {
                    MessageBox.Show("该用户已不可用！");
                    return;
                }
                db.LoginLogs.Add(new LoginLog()
                {
                    LoginTime = DateTime.Now,
                    UserId = user.Id.ToString(),
                    ClientIp = Dns.GetHostName()
                });
                db.SaveChanges();

                LoginInfo.GetLoginInfo(user.Id, user.Name);
                LoginFrm loginFrm = this;
                loginFrm.Hide();
                var main = MainFrm.CreateInstrance();
                main.Show();
            }
            else
            {
                MessageBox.Show("楼层、用户名或者密码错误！");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            base.ProcessCmdKey(ref msg, keyData);
            bool isEnter = false;
            switch (keyData)
            {
                case Keys.Enter:
                    if (userPwd.Focused == true)
                    {
                        loginBtn_Click(null, null);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(userTxt.Text))
                            userPwd.Focus();
                    }
                    isEnter = true;
                    break;

            }
            return isEnter ? true : false;
        }
    }
}
